package com.ysd.lis.service.inv.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.*;
import com.ysd.lis.entity.lab.LabReqMain;
import com.ysd.lis.mapper.inv.InvTransferTypeMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.inv.InvSupplierPageParam;
import com.ysd.lis.request.inv.TransferTypeDto;
import com.ysd.lis.service.SysBasDictDetailService;
import com.ysd.lis.service.inv.InvTransferTypeService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-09-11
 */
@Service
public class InvTransferTypeServiceImpl extends ServiceImpl<InvTransferTypeMapper, InvTransferType> implements InvTransferTypeService {

    @Autowired
    InvTransferTypeMapper transferTypeMapper;
    @Autowired
    SysBasDictDetailService sysBasDictDetailService;
    @Override
    public Result findInvTransferTypePageList(InvSupplierPageParam params) {
        Page<InvTransferType> page = new Page<>();
        page.setCurrent(params.getPageIndex());
        page.setSize(params.getPageSize());
        LambdaQueryWrapper<InvTransferType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvTransferType::getDelFlag, 0);

        if (!StringUtils.isEmpty(params.getSeachValue())) {
            queryWrapper.and(wrapper -> wrapper.like(InvTransferType::getTransferCode, params.getSeachValue()).or().like(InvTransferType::getTransferName, params.getSeachValue()));
        }
        if (ToolsUtils.isNotEmpty(params.getBusinessType())) {
            queryWrapper.eq(InvTransferType::getBusinessType, params.getBusinessType());
        }
        Page<InvTransferType> invTransferTypePage = transferTypeMapper.selectPage(page, queryWrapper);
        List<Map<String, Object>> idTypeList = (List<Map<String, Object>>) sysBasDictDetailService.getSysBasDictList("transferBusinessType").getData();
        for (InvTransferType m : (List<InvTransferType>)invTransferTypePage.getRecords()) {
            if (StringUtils.isNotBlank(m.getBusinessType()) && org.apache.commons.collections4.CollectionUtils.isNotEmpty(idTypeList)) {
                idTypeList.stream().filter(a -> a.get("code").equals(m.getBusinessType())).findFirst().ifPresent(stringObjectMap -> m.setBusinessTypeNa((String) stringObjectMap.get("name")));
            }
        }
        return Result.succ(1, "查询成功", invTransferTypePage);
    }

    @Override
    public Result findInvTransferTypeList(InvSupplierPageParam params) {
        LambdaQueryWrapper<InvTransferType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvTransferType::getDelFlag, 0);

        if (!StringUtils.isEmpty(params.getSeachValue())) {
            queryWrapper.and(wrapper -> wrapper.like(InvTransferType::getTransferCode, params.getSeachValue()).or().like(InvTransferType::getTransferName, params.getSeachValue()));
        }

        if (ToolsUtils.isNotEmpty(params.getBusinessType())) {
            String[] split = params.getBusinessType().split(",");
            queryWrapper.in(InvTransferType::getBusinessType, Arrays.stream(split).toArray() );
        }
        if(ToolsUtils.isNotEmpty(params.getShowFlag())){
            queryWrapper.eq(InvTransferType::getShowFlag,params.getShowFlag());
        }
        List<InvTransferType> invTransferTypes = transferTypeMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", invTransferTypes);
    }

    @Override
    public Result addInvTransferType(InvTransferType transferType) {
        int insert = transferTypeMapper.insert(transferType);
        if(insert>0){
            return Result.succ(1,"添加成功",transferType);
        }else {
            return Result.fail(400,"添加失败","");
        }
    }

    @Override
    public Result editInvTransferType(InvTransferType transferType) {
        int updateById = transferTypeMapper.updateById(transferType);
        if(updateById>0){
            return Result.succ(1,"提交成功",transferType);
        }else {
            return Result.fail(400,"修改失败","");
        }
    }

    @Override
    public Result deleteInvTransferType(InvTransferType transferType) {
        int deleteById = transferTypeMapper.deleteById(transferType.getId());
        if(deleteById>0){
            return Result.succ(1, "删除成功", "");
        }else {
            return Result.fail(400, "删除失败", "");
        }
    }

    @Override
    public Result batchDeleteInvTransferTypeList(TransferTypeDto dto) {
        List<InvTransferType> transferTypeList = dto.getTransferTypeList();
        List<String> ids = transferTypeList.stream().map(InvTransferType::getId).collect(Collectors.toList());
        // //查询供应商下是否有评价登记
        // LambdaQueryWrapper<InvSupplierAppraise> queryWrapper = new LambdaQueryWrapper<>();
        // queryWrapper.eq(InvSupplierAppraise::getDelFlag,0)
        //         .in(InvSupplierAppraise::getSupplierId,ids);
        // Integer cnt = transferTypeMapper.selectCount(queryWrapper);
        // if(cnt>0){
        //     return Result.succ(1, "供应商下有登记评价，不能删除", null);
        // }

        boolean b = this.removeByIds(ids);
        if(b){
            return Result.succ(1, "删除成功", null);
        }else {
            return Result.fail(400,"删除失败","");
        }
    }
}
